package com.tzxs.pay.main.first;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tzxs.pay.BR;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityPendingOrderBinding;
import com.tzxs.pay.databinding.ItemPendingOrderBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.bill.detail.AutTncActivity;
import com.tzxs.pay.main.first.model.PendingOrderBean;
import com.tzxs.pay.main.login.LoginViewModel;
import com.tzxs.pay.util.IAdapter;
import com.tzxs.pay.util.viewmodel.Callback;

//即将过期订单列表
public class PendingOrderActivity extends BaseActivity<ActivityPendingOrderBinding> implements IAdapter.ItemNavigator<PendingOrderBean.DataBean> {
    private LoginViewModel model;

    private int page = 1;//页码

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pending_order;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = ViewModelProviders.of(this).get(LoginViewModel.class);
        dataBinding.pendingToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.setViewModel(model);
        dataBinding.pendingOrderRec.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.pendingOrderRec.setAdapter(new IAdapter<PendingOrderBean, ItemPendingOrderBinding>() {
            @Override
            protected int getLayoutId(int viewType) {
                return R.layout.item_pending_order;
            }

            @Override
            protected void convertListener(@NonNull ItemPendingOrderBinding binding) {
                binding.setNavigator(PendingOrderActivity.this);
            }

            @Override
            protected int getDataBRId(int itemViewType) {
                return BR.item;
            }


        });

        model.snackbarEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                if (s != null) {
                    Toast.makeText(PendingOrderActivity.this, s, Toast.LENGTH_SHORT).show();
                }
            }
        });


        //进入页面获取数据
        model.getNoticeDetail(0, 10, new Callback() {
            @Override
            public void success() {

            }
        });


        //刷新
        dataBinding.refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                //获取列表
                model.getNoticeDetail(page, 10, new Callback() {
                    @Override
                    public void success() {

                    }
                });
                refreshLayout.finishRefresh(1000);


            }
        });
        //加载更多
        dataBinding.refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                ++page;
                //获取列表
                model.getNoticeDetail(page, 10, new Callback() {
                    @Override
                    public void success() {


                    }
                });
                refreshLayout.finishLoadMore(2000);
            }
        });
    }

    @Override
    public void itemClick(PendingOrderBean.DataBean item) {
        Intent intent = new Intent(this, AutTncActivity.class);
        intent.putExtra("orderId", item.getOut_order_no());
        startActivity(intent);
    }
}
